package common.store.web.controller;

import common.store.admin.dto.Category;
import common.store.admin.service.CategoryService;
import common.store.admin.service.impl.CategoryServiceImpl;
import common.store.web.bean.User;
import common.store.web.bean.orderAndProduct;
import common.store.web.service.OrderService;
import common.store.web.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/orderpay")
public class OrderPayServlet  extends HttpServlet {
    OrderService orderService;

    private CategoryService categoryService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        orderService = new OrderServiceImpl();
        categoryService = new CategoryServiceImpl();
        List<Category> categoryList = categoryService.getCategoryList();
        User user= (User)req.getSession().getAttribute("user");
        if(user==null)
        {
            req.getRequestDispatcher("/login.jsp").forward(req , resp);
        }
        String uid = user.getUid();
        List<orderAndProduct> orderList = orderService.getOrderList(uid);
        Double sum=0.0D;
        for (orderAndProduct order: orderList ) {
            sum+=order.getCount()*order.getShopPrice();
        }
        req.setAttribute("categoryList" , categoryList);
        req.setAttribute("cartList" , orderList);
        req.setAttribute("cartMoney" , sum);
        req.getRequestDispatcher("/order_info.jsp").forward(req , resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req , resp);
    }
}
